package com.cs.dao;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cs.entity.Course;
import com.cs.entity.Grade;
import com.cs.entity.Student;
import com.cs.entity.Teacher;

@Repository
@Transactional
public class TeacherDaoImpl {
	@Resource
	private SessionFactory sessionFactory;
	
	//教师登录
	public Teacher teacherLogin(String tno,String tpassword){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Teacher where tno=? and tpassword=?");
		query.setParameter(0, tno);
		query.setParameter(1, tpassword);
		Teacher teacher = (Teacher) query.uniqueResult();
		return teacher;
	}
	//查询个人信息
	public Teacher findTeacherById(Integer id){
		Teacher teacher = this.sessionFactory.getCurrentSession().load(Teacher.class, id);
		return teacher;
	}
	//修改个人信息
	public boolean updateTeacher(Teacher teacher){
		this.sessionFactory.getCurrentSession().update(teacher);
		return true;
	}
	//添加课程
	public int addcourse(Course course){
		int save = (int) this.sessionFactory.getCurrentSession().save(course);
		return save;
	}
	//根据id查询教师开设的课程
	public List<Course> selectCourseByTid(Integer id){
		Query query = this.sessionFactory.getCurrentSession().createQuery
				("select c from Course as c left outer join c.teacher where c.teacher.id=?");
		query.setParameter(0, id);
		List<Course> list = query.list();
		return list;
	}
	//根据学号查询学生
	public Student findStudentBySno(String sno){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Student where sno=?");
		query.setParameter(0, sno);
		Student student = (Student) query.uniqueResult();
		return student;
	}
	//根据课程号查询课程
	public Course findCourseByCno(String cno){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Course where cno=?");
		query.setParameter(0, cno);
		Course course = (Course) query.uniqueResult();
		return course;
	}
	//根据学号和课程号查询成绩
	public Grade findGradeBySnoandCno(String sno,String cno){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Grade where sno=? and cno=?");
		query.setParameter(0, sno);
		query.setParameter(1, cno);
		Grade grade = (Grade) query.uniqueResult();
		return grade;
	}
	//录入成绩（单个）
	public int addGrade(Grade grade){
		int save = (int) this.sessionFactory.getCurrentSession().save(grade);
		return save;
	}
	//根据专业查询学生(模糊查询）
	public List<Student> findStudentBySpecialities(String specialities){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Student where specialities like '%"+specialities+"%'");
		List<Student> list = query.list();
		return list;
	}

}
